import os
import random
import datetime

from django.http import HttpResponse
from django.utils import simplejson as json
from django.template import RequestContext
from django.shortcuts import render_to_response

import cwd.settings as settings
from cwd.gridpoints.models import Gridpoint

# Create your views here.

from django.contrib.gis.geos import *
boundingbox = GEOSGeometry("""{ "type": "Polygon", "coordinates": [ [
                                [-69.0, 20.0], 
                                [-131.0, 20.0], 
                                [-131.0, 50.0], 
                                [-69.0, 50.0], 
                                [-69.0, 20.0] 
                            ]]}""")

def json_list(request):
    data = []
    for g in Gridpoint.objects.all():
    #for g in Gridpoint.objects.filter(point__within=boundingbox):
    #for g in Gridpoint.objects.filter(x__gte=10,x__lte=19,y__gte=20,y__lte=43):
	data.append({
            'x': g.x,
            'y': g.y,
            'lat': g.point.y,
            'lon': g.point.x,
	})
    return HttpResponse(json.dumps({'gridpoints':data}))

def kml_list(request):
    response = HttpResponse(content_type="application/vnd.google-earth.kml+xml")
    qs = Gridpoint.objects.all().kml()
    for g in qs:
	response.write(g.kml)
    return response
